<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑图书</title>
    <%@ include file="/pages/common/head.jsp" %>
    <style type="text/css">
        h1 {
            text-align: center;
            margin-top: 200px;
        }

        h1 a {
            color: red;
        }

        input {
            text-align: center;
        }
    </style>
    <script type="text/javascript">
        $(function () {
            $("#viewBookList").click(function () {
                let pn = ${param.pn};
                let method = "${param.method}";
                if ("add" === method) {
                    pn += 1;
                }
                location.href = "manager/BookServlet?action=page&pn=" + pn;
                return false;
            });

            $("form").submit(function (e) {
                return verification();
            });

            function verification() {
                let msg = "";
                let flag = true;
                if (!verifyPrice()) {
                    msg += "价格、";
                    flag = false;
                }
                if (!verifySales()) {
                    msg += "销量、";
                    flag = false;
                }
                if (!verifyStock()) {
                    msg += "库存、";
                    flag = false;
                }
                msg = msg.substring(0, msg.length - 1);
                if (!flag) {
                    alert(msg + "不合法！");
                }
                return flag;
            }

            function verifyPrice() {
                let pattern = /^\d+(.{1}\d+)*$/;
                return pattern.test($("input[name=price]").val());
            }

            function verifySales() {
                let pattern = /^\d+$/;
                return pattern.test($("input[name=sales]").val());
            }

            function verifyStock() {
                let pattern = /^\d+$/;
                return pattern.test($("input[name=stock]").val());
            }
        });
    </script>
</head>
<body>
<div id="header">
    <img class="logo_img" alt="" src="static/img/logo.gif">
    <span class="wel_word">编辑图书</span>
    <%@ include file="/pages/common/manager_menu.jsp" %>
</div>

<div id="main">
    <form action="manager/BookServlet" method="post">
        <input type="hidden" name="action" value="${param.method}">
        <input type="hidden" name="id" value="${book.id}">
        <input type="hidden" name="pn" value="${param.pn}">
        <%-- 方案二：       ${empty param.id ? "add":"updateById"}--%>
        <%-- 方案三：       ${empty requestScope.book ? "add":"updateById"}--%>
        <table>
            <tr>
                <td>名称</td>
                <td>价格</td>
                <td>作者</td>
                <td>销量</td>
                <td>库存</td>
                <td colspan="2">操作</td>
            </tr>
            <tr>
                <td><input name="name" type="text" value="${book.name}"/></td>
                <td><input name="price" type="text" value="${book.price}"/></td>
                <td><input name="author" type="text" value="${book.author}"/></td>
                <td><input name="sales" type="text" value="${book.sales}"/></td>
                <td><input name="stock" type="text" value="${book.stock}"/></td>
                <td><input type="submit" value="提交"/></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td>
                    <button id="viewBookList">查看图书列表</button>
                </td>
            </tr>
        </table>
    </form>
</div>

<%@ include file="/pages/common/footer.jsp" %>
</body>
</html>